/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gen2phen.labs.actions;

import java.util.ArrayList;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.gen2phen.labs.domain.Investigation;
import org.gen2phen.labs.domain.ObservableFeature;

/**
 *
 * @author pedrolopes
 *
 * GEN2PHEN Semantic Web Labs: Knowledge Mashup
 *
 */
@UrlBinding("/investigation/{$event}/{query}")
public class InvestigationActionBean implements ActionBean {

    private ActionBeanContext context;
    private ArrayList<Investigation> invs = new ArrayList<Investigation>();
    private String query;

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public ArrayList<Investigation> getInvs() {
        return invs;
    }

    public void setInvs(ArrayList<Investigation> invs) {
        this.invs = invs;
    }

    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    public ActionBeanContext getContext() {
        return context;
    }

    /**
     * Default handler will search for all ObservableFeatures assocaited with the queried investigation.
     * These features will be concatened to a String and outputed for client-side Javascript processing:
     * each feature will be searched in Freebase and Flickr, search results will be loaded in the web application.
     *
     * @return A String with all Freebase and Flickr search terms divided by # characters
     */
    @DefaultHandler
    public Resolution search() {
        String results = "";

        // must test if investigations have been loade:, if not, load
        if (org.gen2phen.labs.services.Loader.investigations().size() == 0) {
            org.gen2phen.labs.services.Loader.load();
        }

        invs = org.gen2phen.labs.services.Loader.investigations();

        for (Investigation i : invs) {
            if (i.getName().equals(query)) {
                String[] iis = i.getDescription().split(" ");
                for (String d : iis) {
                    if (d.length() > 3) {
                        // add investigation descriptors to searchable terms list
                        results += d + "#";
                    }
                }

                for (ObservableFeature f : i.getFeatures()) {
                    if (!results.contains(f.getName())) {
                        // add investigation features to searchable terms list
                        results += f.getName() + "#";
                    }
                }

            }
        }

        // delete final # character
        results = results.substring(0, results.length() - 1);
        return new StreamingResolution("text", results);
    }
}
